// If your plugin is direct dependent to the html webpack plugin:
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const { uploadDir } = require('./release/release.js')

class MyPlugin {
  apply(compiler) {
    compiler.hooks.compilation.tap('MyPlugin',(compilation) => {
      HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
        "MyPlugin", // <-- Set a meaningful name here for stacktraces
        (data, cb) => {
          //console.log(data);
          // Tell webpack to move on
          //data.html = data.html.replace(/<script[^<]+bundle[^<]+><\/script>/,'');
          cb(null, data);
        },
      );
    });
    
    compiler.hooks.done.tap("MyPlugin", (compilation) => {
      console.log(`编译结束`);
      copyFile();
      uploadDir('dist/');
    });
    
  }
}

const copyFile = () => {
  let files = glob.sync('./dist/**/*.html');
  files.forEach(res => {
    let endfile = res.replace(/\\/g,'/').replace('dist/','build/');
    fs.copyFileSync(res, endfile);
  })
}
module.exports = MyPlugin;
